agentMET4FOF agents¶
-
class
agentMET4FOF.agents.
AgentMET4FOF
(name='', host=None, serializer=None, transport=None, attributes=None)[source]¶ Base class for all agents with specific functions to be overridden/supplied by user.
Behavioural functions for users to provide are init_parameters, agent_loop and on_received_message. Communicative functions are bind_output, unbind_output and send_output.
-
agent_loop
()[source]¶ User defined method for the agent to execute for loop_wait seconds specified either in self.loop_wait or explicitly via`init_agent_loop(loop_wait)`
To start a new loop, call init_agent_loop(loop_wait) on the agent Example of usage is to check the current_state of the agent and send data periodically
-
bind_output
(output_agent)[source]¶ Forms Output connection with another agent. Any call on send_output will reach this newly binded agent
Adds the agent to its list of Outputs.
Parameters: output_agent (AgentMET4FOF) – Agent to be binded to this agent’s output channel
-
convert_to_plotly
(matplotlib_fig)[source]¶ Internal method to convert matplotlib figure to plotly figure
Parameters: matplotlib_fig (plt.Figure) – Matplotlib figure to be converted
-
handle_process_data
(message)[source]¶ Internal method to handle incoming message before calling user-defined on_received_message method.
If current_state is either Stop or Reset, it will terminate early before entering on_received_message
-
init_agent_loop
(loop_wait=1.0)[source]¶ Initiates the agent loop, which iterates every`loop_wait` seconds
Stops every timers and initiate a new loop.
Parameters: loop_wait (int) – The wait between each iteration of the loop
-
on_init
()[source]¶ Internal initialization to setup the agent: mainly on setting the dictionary of Inputs, Outputs, PubAddr.
Calls user-defined init_parameters() upon finishing.
-
Inputs
¶ Dictionary of Agents connected to its input channels. Messages will arrive from agents in this dictionary. Automatically updated when bind_output() function is called
Type: dict
-
Outputs
¶ Dictionary of Agents connected to its output channels. Messages will be sent to agents in this dictionary. Automatically updated when bind_output() function is called
Type: dict
-
PubAddr_alias
¶ Name of Publish address socket
Type: str
-
PubAddr
¶ Publish address socket handle
Type: str
-
AgentType
¶ Name of class
Type: str
-
current_state
¶ Current state of agent. Can be used to define different states of operation such as “Running”, “Idle, “Stop”, etc.. Users will need to define their own flow of handling each type of self.current_state in the agent_loop
Type: str
-
loop_wait
¶ The interval to wait between loop. Call init_agent_loop to restart the timer or set the value of loop_wait in init_parameters when necessary.
Type: int
-
-
on_received_message
(message)[source]¶ User-defined method and is triggered to handle the message passed by Input.
Parameters: message (Dictionary) – The message received is in form {‘from’:agent_name, ‘data’: data, ‘senderType’: agent_class, ‘channel’:channel_name} agent_name is the name of the Input agent which sent the message data is the actual content of the message
-
pack_data
(data, channel='data')[source]¶ Internal method to pack the data content into a dictionary before sending out.
Parameters: - data (argument) – Data content to be packed before sending out to agents.
- channel (str) – Key of dictionary which stores data
Returns: Packed message data
Return type: dict of the form {‘from’:agent_name, ‘data’: data, ‘senderType’: agent_class, ‘channel’:channel_name}.
-
reset
()[source]¶ This method will be called on all agents when the global reset_agents is called by the AgentNetwork and when the Reset button is clicked on the dashboard.
Method to reset the agent’s states and parameters. User can override this method to reset the specific parameters.
-
send_output
(data, channel='default')[source]¶ Sends message data to all connected agents in self.Outputs.
Output connection can first be formed by calling bind_output. By default calls pack_data(data) before sending out. Can specify specific channel as opposed to default ‘data’ channel.
Parameters: - data (argument) – Data content to be sent out
- channel (str) – Key of message dictionary which stores data
Returns: message
Return type: dict of the form {‘from’:agent_name, ‘data’: data, ‘senderType’: agent_class, ‘channel’:channel_name}.
-
send_plot
(fig=<Figure size 640x480 with 0 Axes>)[source]¶ Sends plot to agents connected to this agent’s Output channel.
This method is different from send_output which will be sent to through the ‘plot’ channel to be handled.
Parameters: fig (Figure) – Can be either matplotlib figure or plotly figure Returns: The message format is {‘from’ Return type: agent_name, ‘plot’: data, ‘senderType’: agent_class}.
-
stop_agent_loop
()[source]¶ Stops agent_loop from running. Note that the agent will still be responding to messages
-
unbind_output
(output_agent)[source]¶ Remove existing output connection with another agent. This reverses the bind_output method
Parameters: output_agent (AgentMET4FOF) – Agent binded to this agent’s output channel
-
-
class
agentMET4FOF.agents.
AgentNetwork
(ip_addr='127.0.0.1', port=3333, connect=False, dashboard_modules=True, dashboard_update_interval=3, log_filename='log_file.csv')[source]¶ Object for starting a new Agent Network or connect to an existing Agent Network specified by ip & port
Provides function to add agents, (un)bind agents, query agent network state, set global agent states Interfaces with an internal _AgentController which is hidden from user
-
add_agent
(name=' ', agentType=<class 'agentMET4FOF.agents.AgentMET4FOF'>, log_mode=True)[source]¶ Instantiates a new agent in the network.
Parameters: - name (str) – Unique name of agent. If left empty, the name will be automatically set to its class name. There cannot be more than one agent with the same name.
- agentType (AgentMET4FOF) – Agent class to be instantiated in the network.
- log_mode (bool) – Default is True. Determines if messages will be logged to background Logger Agent.
Returns: AgentMET4FOF
Return type: Newly instantiated agent
-
agents
()[source]¶ Returns all agent names connected to Agent Network.
Returns: list Return type: names of all agents
-
bind_agents
(source, target)[source]¶ Binds two agents communication channel in a unidirectional manner from source Agent to target Agent
Any subsequent calls of source.send_output() will reach target Agent’s message queue.
Parameters: - source (AgentMET4FOF) – Source agent whose Output channel will be binded to target
- target (AgentMET4FOF) – Target agent whose Input channel will be binded to source
-
connect
(ip_addr='127.0.0.1', port=3333, verbose=True)[source]¶ Parameters: - ip_addr (str) – IP Address of server to connect to
- port (int) – Port of server to connect to
-
get_agent
(agent_name)[source]¶ Returns a particular agent connected to Agent Network.
Parameters: agent_name (str) – Name of agent to search for in the network
-
set_agents_state
(filter_agent=None, state='Idle')[source]¶ Blanket operation on all agents to set their current_state attribute to given state
Can be used to define different states of operation such as “Running”, “Idle, “Stop”, etc.. Users will need to define their own flow of handling each type of self.current_state in the agent_loop
Parameters: - filter_agent (str) – (Optional) Filter name of agents to set the states
- state (str) – State of agents to set
-
set_running_state
(filter_agent=None)[source]¶ Blanket operation on all agents to set their current_state attribute to “Running”
Users will need to define their own flow of handling each type of self.current_state in the agent_loop
Parameters: filter_agent (str) – (Optional) Filter name of agents to set the states
-
set_stop_state
(filter_agent=None)[source]¶ Blanket operation on all agents to set their current_state attribute to “Stop”
Users will need to define their own flow of handling each type of self.current_state in the agent_loop
Parameters: filter_agent (str) – (Optional) Filter name of agents to set the states
-
start_server
(ip_addr='127.0.0.1', port=3333)[source]¶ Parameters: - ip_addr (str) – IP Address of server to start
- port (int) – Port of server to start
-
unbind_agents
(source, target)[source]¶ Unbinds two agents communication channel in a unidirectional manner from source Agent to target Agent
This is the reverse of bind_agents()
Parameters: - source (AgentMET4FOF) – Source agent whose Output channel will be unbinded from target
- target (AgentMET4FOF) – Target agent whose Input channel will be unbinded from source
-
-
class
agentMET4FOF.agents.
DataStreamAgent
(name='', host=None, serializer=None, transport=None, attributes=None)[source]¶ Able to simulate generation of datastream by loading a given DataStreamMET4FOF object.
Can be used in incremental training or batch training mode. To simulate batch training mode, set pretrain_size=-1 , otherwise, set pretrain_size and batch_size for the respective See DataStreamMET4FOF on loading your own data set as a data stream.
-
agent_loop
()[source]¶ User defined method for the agent to execute for loop_wait seconds specified either in self.loop_wait or explicitly via`init_agent_loop(loop_wait)`
To start a new loop, call init_agent_loop(loop_wait) on the agent Example of usage is to check the current_state of the agent and send data periodically
-
init_parameters
(stream=DataStreamMET4FOF(), pretrain_size=None, batch_size=1, loop_wait=1, randomize=False)[source]¶ Parameters: - stream (DataStreamMET4FOF) – A DataStreamMET4FOF object which provides the sample data
- pretrain_size (int) – The number of sample data to send through in the first loop cycle, and subsequently, the batch_size will be used
- batch_size (int) – The number of sample data to send in every loop cycle
- loop_wait (int) – The duration to wait (seconds) at the end of each loop cycle before going into the next cycle
- randomize (bool) – Determines if the dataset should be shuffled before streaming
-
-
class
agentMET4FOF.agents.
MonitorAgent
(name='', host=None, serializer=None, transport=None, attributes=None)[source]¶ Unique Agent for storing plots and data from messages received from input agents.
The dashboard searches for Monitor Agents’ memory and plots to draw the graphs “plot” channel is used to receive base64 images from agents to plot on dashboard
-
memory
¶ Dictionary of format {agent1_name : agent1_data, agent2_name : agent2_data}
Type: dict
-
plots
¶ Dictionary of format {agent1_name : agent1_plot, agent2_name : agent2_plot}
Type: dict
-
on_received_message
(message)[source]¶ Handles incoming data from ‘default’ and ‘plot’ channels.
Stores ‘default’ data into self.memory and ‘plot’ data into self.plots
Parameters: message (dict) – Acceptable channel values are ‘default’ or ‘plot’
-
reset
()[source]¶ This method will be called on all agents when the global reset_agents is called by the AgentNetwork and when the Reset button is clicked on the dashboard.
Method to reset the agent’s states and parameters. User can override this method to reset the specific parameters.
-
update_plot_memory
(message)[source]¶ Updates plot figures stored in self.plots with the received message
Parameters: message (dict) – Standard message format specified by AgentMET4FOF class Message[‘data’] needs to be base64 image string and can be nested in dictionary for multiple plots Only the latest plot will be shown kept and does not keep a history of the plots.
-